package yanglib.application.metadata.message;

import yanglib.message.Message;
import yanglib.session.Reply;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: user
 * Date: 9/6/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class PutReplyMessage extends Reply {
    private boolean successful;
    public PutReplyMessage(ByteBuffer buffer) {
        super(buffer);
    }

    public PutReplyMessage(int sender, long requestID, boolean successful) {
        super(MetadataMessageDeserializer.PUT_REPLY_MESSAGE, sender, requestID);
        this.successful = successful;
    }

    public boolean isSuccessful(){
        return this.successful;
    }

    @Override
    public void serialize(ByteBuffer buffer){

        super.serialize(buffer);
        if(successful)
            buffer.put((byte)1);
        else
            buffer.put((byte)0);
    }

    @Override
    public void deserialize(ByteBuffer buffer) throws IOException {
        super.deserialize(buffer);
        if(buffer.get()==0)
            successful = false;
        else
            successful = true;
    }

    @Override
    public String toString(){
        return "PutReply " +getRequestID() + " result=" + successful;
    }
}
